const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    items: [],
    tabList: [{
        label: '热歌榜'
      },
      {
        label: '新歌榜'
      },
      {
        label: '飙升榜'
      },
      {
        label: '抖音榜'
      },
      {
        label: '电音榜'
      }
    ],
    tabActive: 0,
    loadingVisible: true,
    refreshing: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },
  getData(cb) {
    let word = this.data.tabList[this.data.tabActive].label
    this.setData({
      loadingVisible: true
    })
    wx.request({
      url: app.$api.audioInterface,
      data: {
        sort: word
      },
      success: res => {
        if (res.statusCode == 200 && res.data.data) {
          let data = res.data.data
          data.picurl = app.$util.httpToHttps(data.picurl)
          data.url = app.$util.httpToHttps(data.url)
          this.setData({
            items: [data]
          })
        }
      },
      fail: res => {
        // 
      },
      complete: res => {
        cb && cb(res)
        wx.stopPullDownRefresh();
        this.setData({
          loadingVisible: false
        })
      }
    })
  },
  handleChange({
    detail
  }) {
    this.setData({
      tabActive: detail.key,
      items: []
    });
    wx.pageScrollTo({
      scrollTop: 0
    })
    setTimeout(_ => {
      this.getData()
    }, 200)
  },
  handleRefresh() {
    this.setData({
      refreshing: true,
      items: []
    })
    this.getData(res => {
      this.setData({
        refreshing: false
      })
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    // 
  }
})